package top.rainf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.rainf.domain.vo.BoardMessage;
import top.rainf.service.IBoardMessageService;
import top.rainf.service.IBoardMessageService;
import top.rainf.support.Result;

import java.util.List;

/**
 * @author 星环
 * @date 2023/5/10 16:00:16
 * @description 公告
 */
@RestController
@CrossOrigin
@RequestMapping("/board-message")
public class BoardMessageController {
    @Autowired
    IBoardMessageService boardMessageService;


    /**
     * 获取全部通知的列表
     * @return 返回是否获取成功~
     */
    @RequestMapping("/list")
    public Result list(@RequestParam(required = false) int num){
        List<BoardMessage> list = null;
        if(num != 0){
            list = boardMessageService.selectRecentN(num);
        }else {
            list = boardMessageService.selectAll();
        }
        if(list == null){
            return Result.fail();
        }
        return Result.success().addObj("list",list);
    }

    /**
     * 分页公告列表
     * @param numPerPage 每页几条
     * @param pageNum 第几页
     * @return
     */
    @RequestMapping("/page")
    Result page(int numPerPage,int pageNum){
        List<BoardMessage> list = boardMessageService.selectPage(numPerPage,pageNum);
        List<String> pageList = boardMessageService.getPageListStr(numPerPage,pageNum);
        System.out.println(pageList);
        return Result.success().addObj("list",list).addObj("pageList",pageList);
    }


    /**
     * 根据id获取一个通知详情
     * @return 返回是否获取成功~
     */
    @RequestMapping("/one")
    public Result getOneBoardMessage(int id){
        BoardMessage boardMessage = boardMessageService.readOne(id);
        if(boardMessage == null){
            return Result.fail();
        }
        return Result.success().addObj("data",boardMessage);
    }
    /**
     * 根据id删除一个通知
     ---------------------------
     * @param id 要删除的通知的id
     * @return 返回是否获取成功~
     */
    @RequestMapping("/deleteById")
    public Result deleteById(int id){
        boolean suc = boardMessageService.deleteOneById(id);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("/deleteByIds")
    public Result deleteByIds(List<Integer> idList){
        boolean suc = boardMessageService.deleteAll(idList);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("update")
    public Result update(BoardMessage boardMessage){
        boolean suc = boardMessageService.updateById(boardMessage);
        return suc ? Result.success() : Result.fail();
    }

    /**
     * 直接加载上一条、下一条的内容
     * @param id 当前公告的id
     * @return
     */
    @RequestMapping("/prev-next")
    public Result prevNext(int id){
        BoardMessage prev = boardMessageService.prevOne(id);
        BoardMessage next = boardMessageService.nextOne(id);
        Result result = Result.success();
        return result
                .addObj("prev",prev)
                .addObj("next",next);
    }

}
